﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.Exercitation.Model;
using Yipusoft.Exercitation.BLL;
using Yipusoft.Common.BLL;
using Yipusoft.Account.Model;
using System.Drawing;

public partial class Combat_SoldierRankSeting : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }
    private void Bind()
    {
        UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        if (user == null)
            return;
        Soldier soldier = new Soldier();
        dltSoldiers.DataSource = soldier.GetSoldiersByWhere(" DepartmentID='" + user.DepartmentID + "' And IsDelete='0'", "SoldierID", "Desc", 500, 0);
        dltSoldiers.DataBind();

        chkStation.DataSource = CodeItem.GetList(" ParentID='ZXSBJX00'");
        chkStation.DataTextField = "ItemName";
        chkStation.DataValueField = "ItemID";
        chkStation.DataBind();


    }

    protected void lnbtnSoldier_Command(object sender, CommandEventArgs e)
    {
        lblPrompt.Text = "";
        Soldier soldier = new Soldier();
        SoldierArchiveInfo info = soldier.GetSoldierInfoByID(e.CommandArgument.ToString());
        if (info == null)
            return;

        List<SoldierRankInfo> stationList = SoldierRank.GetList(" SoldierID='" + info.SoldierID + "'");
        foreach (ListItem item in chkStation.Items)
        {
            item.Selected = stationList.Where(o => o.RankID == item.Value).Count() > 0;
        }
        lblStation.Text = "正在修改：" + info.SoldierName + " 的信息";
        btnSave.CommandArgument = info.SoldierID;
   
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(btnSave.CommandArgument))
            return;

        try
        {
            Soldier soldier = new Soldier();
            SoldierArchiveInfo info = soldier.GetSoldierInfoByID(btnSave.CommandArgument.ToString());
            if (info == null)
                return;

            SoldierRank.Delete(new Guid(info.SoldierID));
            foreach (ListItem item in chkStation.Items)
            {
                if (item.Selected)
                {
                    SoldierRankInfo sInfo = new SoldierRankInfo();
                    sInfo.ID = Guid.NewGuid();
                    sInfo.SoldierID = new Guid(info.SoldierID);
                    sInfo.RankID = item.Value;
                    SoldierRank.Insert(sInfo);
                    break;
                }

            }
            lblPrompt.Text = "保存成功";
            lblPrompt.ForeColor = Color.Green;
        }
        catch {
            lblPrompt.Text = "保存失败,请重新补录";
            lblPrompt.ForeColor = Color.Red;
        }
        
        
        btnSave.CommandArgument = "";
        lblStation.Text = "";       
    }
}
